package cn.xinyangtong.manager.controller;

import cn.xinyangtong.common.pojo.DataResult;
import cn.xinyangtong.common.pojo.DefaultResult;
import cn.xinyangtong.manager.service.ItemParamItemService;
import cn.xinyangtong.manager.service.ItemService;
import cn.xinyangtong.manager.entity.Status;
import cn.xinyangtong.pojo.TbItem;
import cn.xinyangtong.pojo.TbItemDesc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 商品管理
 * Created by fengbo on 2017/6/8.
 */
@Controller
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;
    @Autowired
    private ItemParamItemService itemParamItemService;

    /**
     * 根据itemID查询商品规格
     * @param itemId
     * @return
     */
    @RequestMapping("/param/item/{itemId}")
    @ResponseBody
    public DefaultResult showItemParam(@PathVariable Long itemId) {
        return itemParamItemService.getItemParamByItemId(itemId);
    }

    /***
     * 根据商品id查询商品描述信息
     */
    @RequestMapping("/desc/{itemId}")
    @ResponseBody
    public DefaultResult getItemDescByItemId(@PathVariable Long itemId) {
        return itemService.getItemDescByItemId(itemId);
    }

    /***
     * 商品列表查询
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public DataResult<TbItem> getItemList(Integer page, Integer rows) {
        return itemService.getItemList(page, rows);
    }

    /***
     * 添加商品
     */
    @RequestMapping("/save")
    @ResponseBody
    public DefaultResult addItem(TbItem item, String desc, String itemParams) {
        return itemService.addItem(item, desc, itemParams);
    }

    /****
     * 下架商品
     */
    @RequestMapping("/instock")
    @ResponseBody
    public DefaultResult instock(Long[] ids) {
        return itemService.updateStatus(Status.INSTOCK.getStatus(), ids);
    }

    /***
     * 上架商品
     */
    @RequestMapping("/reshelf")
    @ResponseBody
    public DefaultResult reshelf(Long[] ids) {
        return itemService.updateStatus(Status.NORMAL.getStatus(), ids);
    }

    /***
     * 删除商品
     */
    @RequestMapping("/delete")
    @ResponseBody
    public DefaultResult delete(Long[] ids) {
        return itemService.updateStatus(Status.DELETE.getStatus(), ids);
    }

}
